#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
//#include  <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */

#include <linux/gpio/driver.h>

#include <linux/platform_device.h>

#include <linux/slab.h>

//void* kmalloc(size_t size, int flags);

//sudo mknod /dev/mycdev001 c  403 0

/**
 * make
 * make scp
 *
 * test ok on tinkerboard.
 *
 * need to get major number from /proc/devices
 *
 * sudo mknod /dev/mydev1012 c 238 0
 *
 *
 *
 *

 sudo cat /proc/iomem

 ff720000-ff7200ff : /pinctrl/gpio0@ff720000
 ff730000-ff7300ff : /pinctrl/gpio1@ff730000
 ff780000-ff7800ff : /pinctrl/gpio2@ff780000
 ff788000-ff7880ff : /pinctrl/gpio3@ff788000
 ff790000-ff7900ff : /pinctrl/gpio4@ff790000



 RK3399 has 5 groups of GPIO banks: GPIO0~GPIO4, and each group is distinguished by numbers A0~A7, B0~B7, C0~C7, D0~D7.

 地址：
 GPIO0 ： FF72_0000
 GPIO1 ： FF73_0000
 GPIO2 ： FF78_0000
 GPIO3 ： FF78_8000
 GPIO4 ： FF79_0000


 Offset
 GPIO_SWPORTA_DR  : 0x0000
 GPIO_SWPORTA_DDR : 0x0004

 GPIO_1_A7

 Interrupts :
 46       gpio0_int
 47       gpio1_int
 48       gpio2_intr
 49       gpio3_intr
 50       gpio4_intr



 Whenever Port A is configured for interrupts, the data direction must be set to Input

 unsigned long probe_irq_on(void);

 int probe_irq_off(unsigned long);

 **/

//#define MY_MAJOR       499
#define GPIO_1_BASE  0xFF730000
#define  GPIO1_SWPORTA_DR GPIO_1_BASE
#define  GPIO1_SWPORTA_DDR  0xFF730004

#define  GPIO_INTEN   (GPIO_1_BASE + 0x0030)
//CPU number
#define  GPIO_PIN  39

#define LOCATION  7

//#define GPIO_1_A7  0x1

//#define  DIRECTION_OUT 255
//
//#define  LED1_ON    255

#define MY_MAX_MINORS  3
#define   DEVICE_NAME  "device name 499"

struct my_device_data {
	struct cdev cdev;
	char *desc;
	char *content;
};

/**
 *  cat /sys/module/a2/parameters/name
 */

//module_param(name, charp, S_IRUGO);

struct my_device_data devs[MY_MAX_MINORS];

irqreturn_t key_handler(int irq, void *dev_id) {
	pr_err("key_handler \n");
	return IRQ_HANDLED;
}
char *ff;
static int my_open(struct inode *inode, struct file *file) {

	struct my_device_data *p;

//	pr_err(" data %d \n",d);
//	pr_err(" data 1 or 0 ? : %d \n",(d & (GPIO_1_A7 << LOCATION)) >> LOCATION);
	pr_info("a3 my_open  1330 \n");

//    writel(readl(data) | (GPIO_1_A7 << LOCATION), data);

//	switch(MINOR(inode->i_cdev)){
//	default :
//		file->private_data =
//
//	}

	p = container_of(inode->i_cdev, struct my_device_data, cdev);
	p->desc = "my dec 789";
	file->private_data = p;

//	p->content = (char*) kmalloc(1024, GFP_KERNEL);
//	memcpy(p->content,
//			"The kmalloc allocation engine is a powerful tool and easily learned because of its similarity to malloc. "
//					"The function is fast (unless it blocks) and doesn’t clear the memory it obtains; the allocated region still holds its previous content.[1] "
//					"The allocated region is also contiguous in physical memory",
//			1024);
//
//	pr_err("content: %s\n", p->content);
	return 0;
}

static unsigned char mem[100];

unsigned int num;

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	size_t my_size = 100;
	ssize_t len = min(my_size - (ssize_t )*offset, size);
	pr_info("a3 my_write\n");

	pr_info("write");
	if (len <= 0)
		return 0;
//
	/* read data from user buffer to my_data->buffer */
	if (copy_from_user(mem + *offset, user_buffer, len))
		return -EFAULT;
	*offset += len;
	pr_info("r: %s", mem);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *buffer, size_t count,
		loff_t *offset) {
	int p = *offset;
	uint8_t *data = "haha,1012\n";
	size_t datalen = strlen(data);
	struct my_device_data *device_data = filp->private_data;
	pr_info("desc: %s\n", device_data->desc);

	if (p > datalen) {
		return 0;
	}
	if (count > datalen - p) {
		count = datalen - p;
	}
	if (copy_to_user(buffer, data + *offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

int my_close(struct inode *inode, struct file *file) {
	pr_info("a3 my_close\n");
	return 0;
}

const struct file_operations my_fops = { .owner = THIS_MODULE, .open = my_open,
		.read = my_read, .write = my_write, .release = my_close,
		};

static dev_t mydev;

static __init int my_init(void) {
	int ret,i,err;
		unsigned int __iomem *direction = ioremap(GPIO1_SWPORTA_DDR, 4);
		unsigned int a = readl(direction);
		unsigned int __iomem *irq = ioremap(GPIO_INTEN, 4);

	// set direction in
		writel(readl(direction) & (~(1 << LOCATION)), direction);
		//just check
		a = readl(direction);
		pr_err("direction: %d \n", a);

		pr_err("direction 1 or 0  ? : %d \n", (a & (1 << LOCATION)) >> LOCATION);

	// enable Interrupt
		writel(readl(irq) | (1 << LOCATION), irq);

		pr_err("Interrupt 1 or 0  ? : %d \n",
			(readl(irq) & (1 << LOCATION)) >> LOCATION);

		num = gpio_to_irq(GPIO_PIN);

		pr_info("irq_num:%d\n",num);

		ret = request_irq(num, key_handler, IRQF_TRIGGER_LOW,
					"key_interrupt", NULL);

		if (ret < 0) {
				pr_err("request_irq  failed \n");
				return ret;
			}

	pr_info("a3 init_module\n");
	err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i)
, 1);
	}
	return 0;
}

static void __exit my_exit(void) {
		int i;
		free_irq(num, NULL);
		pr_info("a3 cleanup_module\n");for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
		}
	unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
